﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.Core.Contents.Settings;
using SchoolPointe.Menus.Enums;
using SchoolPointe.Menus.Models;
using Orchard.ContentManagement.Drivers;
using SchoolPointe.Menus.ViewModels;
using SchoolPointe.RelatedContent.Drivers;
using SchoolPointe.RelatedContent.Services;
using SchoolPointe.RelatedContent.Models;
using SchoolPointe.RelatedContent.Settings;
using SchoolPointe.Menus.Services;
using Orchard.ContentManagement.Handlers;

namespace SchoolPointe.Menus.Drivers
{
    public class BetterMenuItemDriver : ContentPartDriver<BetterMenuItemPart>
    {
        private readonly IRelatedContentService _relatedContentService;
        private readonly IContentManager _contentManager;
        private readonly IEnumerable<IRelatedContentDriver> _relatedContentDrivers;
        private readonly IMenuService _menuService;

        public BetterMenuItemDriver(IRelatedContentService relatedContentService, IContentManager contentManager, IEnumerable<IRelatedContentDriver> relatedContentDrivers, IMenuService menuService)
        {
            _relatedContentService = relatedContentService;
            _contentManager = contentManager;
            _relatedContentDrivers = relatedContentDrivers;
            _menuService = menuService;
        }

        protected override string Prefix
        {
            get
            {
                return "MenuItem.Editor";
            }
        }

        protected override DriverResult Editor(BetterMenuItemPart part, dynamic shapeHelper)
        {
            return Editor(part, null, shapeHelper);
        }

        protected override DriverResult Editor(BetterMenuItemPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
        {
            dynamic vm;
            string template;

            //validation isn't working properly if I use a downcast (MenuItemViewModel or dynamic model, so I'm doing a crappy workaround here)
            if (part.MenuItemType == MenuItemType.CustomLink)
            {
                var viewModel = new MenuItemCustomLinkViewModel(part);
                if (updater != null)
                {
                    updater.TryUpdateModel(viewModel, Prefix, null, null);
                    viewModel.UpdatePart(part);
                }
                vm = viewModel;
                template = "MenuItemCustomLinkViewModel";

            }
            else if (part.MenuItemType == MenuItemType.ContentItem)
            {                
                var viewModel = new MenuItemContentItemViewModel(part);
                if (updater != null)
                {
                    updater.TryUpdateModel(viewModel, Prefix, null, null);
                    viewModel.UpdatePart(part);
                }
                vm = viewModel;
                template = "MenuItemContentItemViewModel";
            }
            else            
                return null;                  

            return ContentShape("MenuItem_Editor", () => 
                shapeHelper.EditorTemplate(
                    Model: vm,
                    TemplateName: template,
                    Prefix: Prefix));
        }


        protected override void Importing(BetterMenuItemPart part, ImportContentContext context)
        {
            part.Text = context.Attribute(part.PartDefinition.Name, "Text");
            part.SubTitle = context.Attribute(part.PartDefinition.Name, "SubTitle");
            part.Url = context.Attribute(part.PartDefinition.Name, "Url");
            part.MenuItemType = (MenuItemType)Enum.Parse(typeof(MenuItemType), context.Attribute(part.PartDefinition.Name, "MenuItemType"), true);
            part.SortOrder = Convert.ToInt32(context.Attribute(part.PartDefinition.Name, "SortOrder"));            
        }

        protected override void Exporting(BetterMenuItemPart part, ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("Text", part.Text);
            context.Element(part.PartDefinition.Name).SetAttributeValue("SubTitle", part.SubTitle);
            context.Element(part.PartDefinition.Name).SetAttributeValue("Url", part.Url);
            context.Element(part.PartDefinition.Name).SetAttributeValue("MenuItemType", part.MenuItemType);
            context.Element(part.PartDefinition.Name).SetAttributeValue("SortOrder", part.SortOrder);
        }
    }
}